package com.firstjianmo.fragmentbestpractice.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.firstjianmo.fragmentbestpractice.NewsContentActivity;
import com.firstjianmo.fragmentbestpractice.R;
import com.firstjianmo.fragmentbestpractice.adapter.NewsAdapter;
import com.firstjianmo.fragmentbestpractice.model.News;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 2016/12/3.
 */

public class NewsTitleFragment extends Fragment implements AdapterView.OnItemClickListener {


    private ListView newsTitleListView;
    private List<News> newsGroup;
    private NewsAdapter adapter;
    private boolean isTowPane;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        newsGroup = getNews();
        adapter = new NewsAdapter(context, R.layout.news_item, newsGroup);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_title_frag, container, false);
        newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view);
        newsTitleListView.setAdapter(adapter);
        newsTitleListView.setOnItemClickListener(this);
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isTowPane = getActivity().findViewById(R.id.news_content_layout) != null; // 如果能找到说明加载的是大屏幕机型
        //isTowPane = false;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        News news = newsGroup.get(position);
        if (isTowPane) {
            NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
            newsContentFragment.refresh(news);
        } else {
            NewsContentActivity.actionStart(getActivity(), news);
        }
    }


    public List<News> getNews() {
        List<News> newsList = new ArrayList<>();

        News news = new News();
        news.setTitle("123124edwsfdfhnuidfs");
        news.setContent("sfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);
        news = new News();
        news.setTitle("1dsfsggd23124edwsfdfhnuidfs");
        news.setContent("aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        news = new News();
        news.setTitle("12312567568igfh3451dsfsggd23124edwsfdfhnuidfs");
        news.setContent("1231dfgsdfbvc47hj4t45t43554aaaaaasfoighidoghjdiosgoiuhjsdfoigsdjiofghsdoifgjhsdiubnjkneaiuorgh as uyob sdvuiobhnadufighuiguibnsdibnsdfigahngubiadfhnbsznvbjksdnfiueafghnuadifbnadkvjbnadjkvbndkjv");
        newsList.add(news);

        return newsList;
    }
}
